/**
 * Sample React Native App
 * https://github.com/facebook/react-native
 * @flow
 */

import React, { Component } from 'react';
import XPay from 'react-native-puti-pay'
import {
  AppRegistry,
  StyleSheet,
  Text,
  View,
    TouchableOpacity
} from 'react-native';

export default class payDemo extends Component {
  render() {
    return (
      <View style={styles.container}>
        <Text style={styles.welcome}>
          Welcome to React Native!
        </Text>
        <Text style={styles.instructions}>
          To get started, edit index.ios.js
        </Text>
        <Text style={styles.instructions}>
          Press Cmd+R to reload,{'\n'}
          Cmd+D or shake for dev menu
        </Text>
        <TouchableOpacity onPress={this.goPay.bind(this,'0.01')}>
          <Text style={styles.welcome}>点击跳转</Text>
        </TouchableOpacity>
      </View>
    );
  }
  goPay(num){
      fetch('https://wxpay.wxutil.com/pub_v2/app/app_pay.php?plat=ios')
          .then((response) => response.text())
          .then((responseText) => {
              const json = JSON.parse(responseText);
              alert(json.appid)
              XPay.setWxId(json.appid)
              //设置	支付宝URL Schemes
              // XPay.setAlipayScheme(scheme)

              //支付宝支付
              //orderInfo是后台拼接好的支付参数
              // XPay.alipay(orderInfo,(res)=>console.log(res))
              //微信支付
              //这些参数都是由后台生成的
              let params = {
                  partnerId:json.partnerid,
                  prepayId: json.prepayid,
                  packageValue:json.package,
                  nonceStr: json.noncestr,
                  timeStamp:json.timestamp,
                  sign: json.sign,
              }
              XPay.wxPay(params,(res)=>console.log(res))
          })
          .catch((error) => {
              console.error(error);
          });


  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
    backgroundColor: '#F5FCFF',
  },
  welcome: {
    fontSize: 20,
    textAlign: 'center',
    margin: 10,
  },
  instructions: {
    textAlign: 'center',
    color: '#333333',
    marginBottom: 5,
  },
});

AppRegistry.registerComponent('payDemo', () => payDemo);
